package cn.yhbk.web.modules.manager.Controller;

import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.VO.Details.UpdateDetail;
import cn.yhbk.web.modules.manager.Service.IGpaCheckDetailsService;
import cn.yhbk.web.modules.manager.Service.ITbGpaDetailsResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-09-10
 */
@RestController
@Api(tags = "综测详情管理接口")
@RequestMapping("/gpaCheckDetails")
public class GpaCheckDetailsController {

    @Autowired
    IGpaCheckDetailsService detailsService;

    @GetMapping("/manage/{checkId}")
    public CommonResult getDetails(@PathVariable Long checkId){
        detailsService.getDetailsByCheckIds(Collections.singletonList(checkId));
        return null;
    }

    @ApiOperation("修改详情状态")
    @PutMapping("/manage/{detailId}")
    public CommonResult updateDetails(@PathVariable Long detailId, @RequestBody Map<String, Object> data){

        boolean res;

        boolean judge = (boolean) data.get("judge");
        String refuseReason = (String) data.get("refuseReason");

        if(judge){
            //通过
            res = detailsService.pass(detailId);
        }else{
            //拒绝通过
            res = detailsService.refuse(detailId,refuseReason);
        }
        if(res){
            return CommonResult.success("操作成功!");
        }else{
            return CommonResult.failed();
        }

    }

    @ApiOperation("修改详情")
    @PutMapping("/stu/{detailId}")
    public CommonResult updateDetail(@PathVariable Long detailId ,@RequestBody UpdateDetail updateDetail){

        //判断该详情是否是属于当前用户的
        Long adminId = MyUtil.getCurrentAdminId();
        boolean detailJudge = detailsService.judgeDetailIdStu(adminId,detailId);
        if(!detailJudge)return CommonResult.failed("更新失败!");
        boolean success = detailsService.updateDetail(detailId,updateDetail);
        return CommonResult.success("更新成功!");
    }


}
